package com.coffee.admin.controller.platform;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.PlatformLoginCheck;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.result.Result;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DateUtil;
import com.coffee.statistics.api.query.StatisticsQuery;
import com.coffee.statistics.api.req.StatisticsReq;
import com.coffee.statistics.api.result.StatisticsResult;
import com.coffee.statistics.api.result.TotalStatisticsResult;
import com.coffee.statistics.api.service.StatisticsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@RestController
@RequestMapping("/platform/statistics")
public class PlatformStatisticsController extends BaseController {

    @DubboReference(check = false)
    private StatisticsService statisticsService;

    @OperLog(operModul = "平台-报表统计-交易统计报表", operDesc = "查询交易统计报表")
    @PlatformLoginCheck(alias = "PlatformStatisticsListAlias")
    @RequestMapping(value = "/findList")
    public Result<Page<StatisticsResult>> findList(HttpServletRequest request, @RequestBody StatisticsReq params) {
        Result<Page<StatisticsResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        StatisticsQuery statisticsQuery = new StatisticsQuery();
        BeanUtilExt.copyProperties(statisticsQuery, params);
        result.setData(statisticsService.getPager(statisticsQuery, pagerInfo));
        return result;
    }

    @OperLog(operModul = "平台-报表统计-交易统计报表", operDesc = "查询交易统计报表")
    @PlatformLoginCheck(alias = "PlatformStatisticsListAlias")
    @RequestMapping(value = "/findAccumulate")
    public Result<TotalStatisticsResult> findAccumulate(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        String date = jsonObject.getString("date");
        Result<TotalStatisticsResult> result = Result.success(null);
        result.setData(statisticsService.getTotalStatisticsResult(date));
        return result;
    }

    @OperLog(operModul = "平台-报表统计-交易统计报表", operDesc = "查询交易统计报表")
    @RequestMapping(value = "/addByDay")
    public Result<String> addByDay(HttpServletRequest request) {
        Result<String> result = Result.success(null);
        for (int i = 0; i <= 400; i++) {
            Date minDate = DateUtil.setMinDate(DateUtil.addDays(new Date(), -i));
            Date maxDate = DateUtil.setMaxDate(DateUtil.addDays(new Date(), -i));
            statisticsService.addByDay(minDate, maxDate);
        }
        result.setData("success");
        return result;
    }


}
